﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xnaHelper.Input;
using xnaHelper.Helpers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

namespace xnaHelper.Controls
{
    public class ImageButton
    {
        public Vector2 Position;
        private List<Texture2D> Images;
        private List<Texture2D> HoverImages;
        private List<Texture2D> ClickedImages;
        public float ZIndex;
        public TimeSpan AnimationSpeed = new TimeSpan(0, 0, 0, 0, 50);
        private TimeSpan TimeOfLastFrameChange = new TimeSpan();
        private List<Texture2D> CurrentAnimationList;
        private int currentAnimationIndex = 0;
        private ButtonStatus State = ButtonStatus.Normal;
        private bool Loop;
        public string Id { get; set; }
        private MouseHelper _mouse;

        public delegate void ButtonEvent(ImageButton sender);
        public event ButtonEvent ButtonClicked;

        public ImageButton(string id, Vector2 position, List<Texture2D> images, List<Texture2D> hoverImages, List<Texture2D> clickedImages, float zIndex, bool loop)
        {
            if (images.Count == 0)
                throw new Exception("Must provide at least 1 image");

            Id = id;
            Position = position;
            Images = images;
            HoverImages = hoverImages;
            ClickedImages = clickedImages;
            ZIndex = zIndex;
            CurrentAnimationList = Images;
            Loop = loop;
            _mouse = new MouseHelper();
            _mouse.MouseMoved += new MouseHelper.MouseMovementEvent(_mouse_MouseMoved);
            _mouse.MouseButtonReleased += new MouseHelper.MouseButtonEventEvent(_mouse_MouseButtonReleased);
        }

        void _mouse_MouseButtonReleased(MouseHelper sender, MouseButtons button, MouseState state)
        {
            if (IntersectsWithCoordinates(state.X, state.Y) && State != ButtonStatus.Clicked)
            {
                State = ButtonStatus.Clicked;
                currentAnimationIndex = 0;
                CurrentAnimationList = ClickedImages;
            }
        }

        void _mouse_MouseMoved(MouseHelper sender, MouseState state)
        {
            if (State == ButtonStatus.Hover && IntersectsWithCoordinates(state.X, state.Y) == false)
            {
                State = ButtonStatus.Normal;
                currentAnimationIndex = 0;
                CurrentAnimationList = Images;
            }
            else if (State == ButtonStatus.Normal && IntersectsWithCoordinates(state.X, state.Y))
            {
                State = ButtonStatus.Hover;
                currentAnimationIndex = 0;
                CurrentAnimationList = HoverImages;
            }
        }

        public void Draw(GameTime gameTime)
        {
            GraphicsHelper.spriteBatch.Draw(CurrentAnimationList[currentAnimationIndex], Position, null, Color.White, (float)0, new Vector2(0, 0), (float)1, SpriteEffects.None, ZIndex);
        }

        public void Update(GameTime gameTime)
        {
            if (gameTime.TotalRealTime - TimeOfLastFrameChange >= AnimationSpeed)
            {
                if (currentAnimationIndex + 1 == CurrentAnimationList.Count)
                {
                    if (State == ButtonStatus.Clicked)
                    {
                        currentAnimationIndex = 0;
                        CurrentAnimationList = HoverImages;
                        State = ButtonStatus.Hover;
                        if (ButtonClicked != null)
                            ButtonClicked(this);
                    }
                    else if (Loop)
                        currentAnimationIndex = 0;
                }
                else
                    currentAnimationIndex += 1;
                TimeOfLastFrameChange = gameTime.TotalRealTime;
            }
            _mouse.Update();
        }

        public void Initialize()
        {
        }

        public float Width
        {
            get
            {
                return Images[0].Width;
            }
        }

        public float Height
        {
            get
            {
                return Images[0].Height;
            }
        }

        private bool IntersectsWithCoordinates(int x, int y)
        {
            if (x > Position.X && x < Position.X + Width)
                if (y > Position.Y && y < Position.Y + Height)
                    return true;

            return false;
        }
    }

    public enum ButtonStatus
    {
        Normal,
        Hover,
        Clicked
    }
}
